package com.hmkj.core.eolinker.base;


import com.hmkj.boot.utils.SpringContextUtils;
import com.hmkj.core.constant.ConfigNID;
import com.hmkj.core.eolinker.models.EolinkerResult;
import com.hmkj.core.service.cache.RedisService;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * Created by 陌上人 on 2077/7/7 18:09.
 */
public class EndPoint {

    private static RedisService redisService = SpringContextUtils.getBean(RedisService.class);

    public static String doGet(String route) {
        String baseEndpoint = redisService.get(ConfigNID.BASE_END_POINT);
        String result = "";
        HttpGet httpRequest = new HttpGet(baseEndpoint + route);
        EolinkerResult eolinkerResult;
        try {
            //使用DefaultHttpClient类的execute方法发送HTTP GET请求，并返回HttpResponse对象。
            HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);//其中HttpGet是HttpUriRequst的子类
            if (httpResponse.getStatusLine().getStatusCode() == 200) {
                HttpEntity httpEntity = httpResponse.getEntity();
                result = EntityUtils.toString(httpEntity);//取出应答字符串
                // 一般来说都要删除多余的字符
                result.replaceAll("\r", "");//去掉返回结果中的"\r"字符，否则会在结果字符串后面显示一个小方格
            } else
                httpRequest.abort();
        } catch (IOException e) {
            e.printStackTrace();
            result = e.getMessage();
        }
        return result;
    }

    public static void main(String[] args) {
        System.out.println(doGet("/index?stock_code=1A0001"));
    }
}
